package cn.usian.controller;

import cn.usian.pojo.Emp;
import cn.usian.service.EmpService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import java.util.List;

@Controller
@RequestMapping("/emp")
public class EmpController {
    //注入empservice
    @Autowired
    private EmpService empService;

   /* //查询
   @GetMapping("/findAll")
    public String findAll(Model model){
        List<Emp> list = empService.findAll();
        model.addAttribute("list",list);
        return "emp";
    }*/


   @GetMapping("emp")
   public String findAll(Model model, @RequestParam(required = true,defaultValue = "1")Integer pageNum){
       PageHelper.startPage(pageNum,3);
       List<Emp> list = empService.findAll();
       PageInfo<Emp> pageInfo = new PageInfo<>(list);
       model.addAttribute("list",pageInfo);
       return "emp";


   }
    //添加
    @PostMapping("add")
    public String add(Emp emp){
        empService.add(emp);
        return "redirect:findAll";
    }

    @RequestMapping("toadd")
    public String toadd(){
        return "add";
    }

    //回显
    @GetMapping("findById")
    public String findById(Model model,int id){
        Emp emp  = empService.findById(id);
        model.addAttribute("emp",emp);
        return "update";
    }

    //修改
    @PostMapping("update")
    public String update(Emp emp){
        empService.update(emp);
        return "redirect:findAll";
    }

   @GetMapping("del")
   public String del(int id){
       empService.del(id);
       return "redirect:findAll";
   }
}
